package activemq.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/*
 * 2019/1/13 15:10
 * by activemq
 */
public class Subscriber1 {
    public static void main(String[] args) throws JMSException {
        TopicConnectionFactory factor=new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
        TopicConnection topicConnection = factor.createTopicConnection();
        topicConnection.start();
        TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

        Destination destination = topicSession.createTopic("topic1");
        MessageConsumer consumer = topicSession.createConsumer(destination);
        System.out.println("Subscriber1 Listening...");
        for(int i=0;i<1000;i++)
        {
            TextMessage message = (TextMessage) consumer.receive();
            if(message!=null)
            {
                System.out.println(message.getText());
            }
        }
        topicConnection.close();
    }
}
